<?php
class Friend extends Zend_Db_Table_Abstract
{
    protected $_name = 'profile_ref';

    const VALID = 0;
    const BLOCK = 1;

    public function authViewProfile($userId, $frdId=0)
    {
        //自己看自己
        if ( (0 == $frdId) || ($userId == $frdId) )
        {
            return true;
        }
        $ids1 = $this->getFriendIds($frdId);

        //当前用户在被查看用户的好友列表中
        if (false == in_array($userId, $ids1))
        {
            $ids2 = $this->getFriendIds($userId);

            //当前用户和被查看用户有共同的好友
            $res = array_intersect($ids1, $ids2);
            if (empty($res))
            {
                return false;
            }
        }
        return true;
    }

    //请求添加好友
    public function add($userId, $friendId)
    {/*{{{*/
        if ($this->isAdded($userId, $friendId))
        {
            throw new FriendException(FriendException::USER_ALREADY_ADDED);
        }
        $this->addIdol($userId, $friendId);
    }/*}}}*/

    //接受请求
    public function accept($userId, $friendId)
    {/*{{{*/
        if ($this->isAdded($userId, $friendId))
        {
            throw new FriendException(FriendException::USER_ALREADY_ACCEPTED);
        }
        $this->addIdol($userId, $friendId);
    }/*}}}*/

    //拒绝请求
    public function deny($userId, $friendId)
    {/*{{{*/
        $db = &$this->getAdapter();
        $where = $db->quoteInto('idolId= ? and stalkerId=?', $userId, $friendId);
        return $this->delete($where);
    }/*}}}*/

    //删除好友
    public function remove($userId, $friendId)
    {/*{{{*/
        $db = &$this->getAdapter();
        $where = $db->quoteInto('stalkerId=?', $userId).$db->quoteInto('and idolId=?', $friendId);
        return $this->delete($where);
    }/*}}}*/

    //屏蔽好友
    public function block($userId, $friendId)
    {
        //todo
        throw new SysException('not impl yet!');
    }

    private function getFriendIds($userId)
    {/*{{{*/
        $stalkers = $this->loadStalkers($userId);
        $idols = $this->loadIdols($userId);
        return array_intersect_assoc($stalkers, $idols);
    }/*}}}*/

    //获取好友
    public function getFriends($userId)
    {/*{{{*/
        $ids = $this->getFriendIds($userId);        
        if (false == empty($ids))
        {
            $profile = new Profile();
            $friends = $profile->batchGetByUserId($ids);
            return $friends->toArray();
        }
        return array();
    }/*}}}*/

    //获取好友请求
    public function getFriendRequests($userId)
    {/*{{{*/
        $stalkers = $this->loadStalkers($userId);
        $idols = $this->loadIdols($userId);
        foreach ($stalkers as $stalker)
        {
            if (false == in_array($stalker, $idols))
            {
                $params[] = $stalker;
            }
        }
        
        if (false == empty($params))
        {
            $profile = new Profile();
            $friends = $profile->batchGetByUserId($params);
            return $friends->toArray();
        }
        return array();
    }/*}}}*/


    //todo: cache
    private function loadx($queryColumn, $resultColumn, $userId)
    {/*{{{*/
        $db = &$this->getAdapter();

        $sql = $db->quoteInto('select '.$resultColumn.' as userId from '.$this->_name.' where '.$queryColumn.'=?', $userId);
        $result = $db->query($sql);
        $res = $result->fetchAll();
        $ids = array();
        foreach ($res as $info)
        {
            $ids[] = $info['userId'];
        }
        return $ids;
    }/*}}}*/

    private function loadStalkers($userId)
    {/*{{{*/
        return $this->loadx('idolId', 'stalkerId', $userId);
    }/*}}}*/

    private function loadIdols($userId)
    {/*{{{*/
        return $this->loadx('stalkerId', 'idolId', $userId);
    }/*}}}*/

    private function addStalker($userId, $stalkerId)
    {/*{{{*/
        $data = array('stalkerId'=> $stalkerId, 'idolId'=>$userId);
        return $this->insert($data);
    }/*}}}*/

    private function addIdol($userId, $idolId)
    {/*{{{*/
        $data = array('idolId'=> $idolId, 'stalkerId'=>$userId);
        $this->insert($data);
    }/*}}}*/

    private function isAdded($userId, $friendId)
    {/*{{{*/
        $db = &$this->getAdapter();
        $res = $db->fetchOne('select count(*) as cnt from '.$this->_name.' where idolId=:idolId and stalkerId=:stalkerId', 
            array('idolId'=>$friendId, 'stalkerId'=>$userId));
        return ($res > 0);
    }/*}}}*/
}
